#include "API.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define CHECK_INIT(ptr) do { if (!pServer){return STATUS_FAIL;} }while (0)

    ELP_API LPVOID ELP_Create()
    {
        IOCPService * pServer = new IOCPService;
        if (!pServer) return NULL;
        return pServer;
    }

    ELP_API STATUS ELP_Close(IOCPService * pServer, ENDPOINT_ID id)
    {
        CHECK_INIT(pServer);
        return pServer->close(id);
    }

    ELP_API ENDPOINT_ID ELP_Connect(IOCPService* pServer, const char *szIp, int port)
    {
        CHECK_INIT(pServer);
        return pServer->connect(szIp, port);
    }

    ELP_API STATUS ELP_SendMsg(IOCPService* pServer, ENDPOINT_ID id, const char *buf, int len)
    {
        CHECK_INIT(pServer);
        return pServer->sendMsg(id, buf, len);
    }

    ELP_API STATUS ELP_ServiceInit(IOCPService* pServer)
    {
        CHECK_INIT(pServer);
        return pServer->init();
    }
    ELP_API STATUS ELP_SetEventHandler(IOCPService* pServer, TCPEventHandler* pEventHandler)
    {
        CHECK_INIT(pServer);
        return pServer->setEventHandler(pEventHandler);
    }
    ELP_API STATUS ELP_Destory(IOCPService* pServer)
    {
        CHECK_INIT(pServer);
        delete pServer;
        pServer = NULL;
        return STATUS_SUCCESS;
    }

#ifdef __cplusplus
}
#endif